﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTrader.Chart
{
    public partial class FutOptBasisChartFrm : System.Windows.Forms.Form
    {
        public FutOptBasisChartFrm()
        {
            InitializeComponent();
        }

        // Cybos Plus Data Set Member
        private ETDataSet m_CPDataSet = null;
        public EasyTrader.ETDataSet CPDataSet
        {
            get { return m_CPDataSet; }
            set { m_CPDataSet = value; }
        }
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private void InitTimer()
        {
            timer.Interval = GlobalVar.ChartRefreshInterval;
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Start();
        }

        private void InitControl()
        {
            FutOptBasisChartCtrl.Series["베이시스"].ToolTip = "#VALX : #VAL";
        }
        // 여기서 차트를 갱신해 준다.
        private void OnTimerTick(object sender, EventArgs e)
        {
            // 차트에 데이터를 대응시켜 준다.
            SetChartData();
        }

        private void SetChartData()
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null)
                return;

            FutOptBasisChartCtrl.Series["베이시스"].Points.Clear();
            EasyTrader.DataSet.FutOptChartTable dataTable = m_CPDataSet.GetFutOptChartTable();
            double maxValue = -10000.0;
            double minValue = 10000.0;
            int rowCount = dataTable.GetRowCount();
            if (rowCount == 0)
                return;

            int minIndex = 0;
            int maxIndex = 0;
            double volume = 0;
            for (int i = 0; i < rowCount; i++)
            {
                DataRow row = dataTable.Rows[i];
                try
                {
                    // 값이 없을 경우는 넘어 간다.
                    if (row["베이시스"] == DBNull.Value)
                        continue;

                    volume = Convert.ToDouble(row["베이시스"]);
                    FutOptBasisChartCtrl.Series["베이시스"].Points.AddXY(i, volume);
                    FutOptBasisChartCtrl.Series["베이시스"].Points[i].XValue = i;
                    FutOptBasisChartCtrl.Series["베이시스"].Points[i].AxisLabel = row["시각"].ToString();

                    if (volume != 0 && volume > maxValue)
                    {
                        maxValue = volume;
                        maxIndex = i;
                    }
                    if (volume != 0 && volume < minValue)
                    {
                        minValue = volume;
                        minIndex = i;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            double valueHeight = maxValue / 1;
            maxValue = (valueHeight + 1);

            if (maxValue >= minValue)
            {
                FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.Minimum = minValue;
                FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.Maximum = maxValue;
            }
        }

        public FutOptBasisChartFrm(ETDataSet a_DataSet)
        {
            InitializeComponent();
            m_CPDataSet = a_DataSet;
            InitControl();
        }

        private void InitChart()
        {
            FutOptBasisChartCtrl.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(223)))), ((int)(((byte)(193)))));
            FutOptBasisChartCtrl.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            FutOptBasisChartCtrl.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(181)))), ((int)(((byte)(64)))), ((int)(((byte)(1)))));
            FutOptBasisChartCtrl.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            FutOptBasisChartCtrl.BorderlineWidth = 2;
            FutOptBasisChartCtrl.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.Interval = 10;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.Interval = 10;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.Interval = 1;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.MinorGrid.Interval = 0.5;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.Interval = 1;

            FutOptBasisChartCtrl.ChartAreas["Price"].Area3DStyle.IsClustered = true;
            FutOptBasisChartCtrl.ChartAreas["Price"].Area3DStyle.Perspective = 10;
            FutOptBasisChartCtrl.ChartAreas["Price"].Area3DStyle.IsRightAngleAxes = false;
            FutOptBasisChartCtrl.ChartAreas["Price"].Area3DStyle.WallWidth = 0;
            FutOptBasisChartCtrl.ChartAreas["Price"].Area3DStyle.Inclination = 15;
            FutOptBasisChartCtrl.ChartAreas["Price"].Area3DStyle.Rotation = 10;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.IsLabelAutoFit = true;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = false;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.IsLabelAutoFit = true;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.IsStartedFromZero = true;
            //FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.LabelAutoFitMaxFontSize;
            FutOptBasisChartCtrl.ChartAreas["Price"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            FutOptBasisChartCtrl.ChartAreas["Price"].BackSecondaryColor = System.Drawing.Color.White;
            FutOptBasisChartCtrl.ChartAreas["Price"].BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            FutOptBasisChartCtrl.ChartAreas["Price"].BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutOptBasisChartCtrl.ChartAreas["Price"].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            FutOptBasisChartCtrl.ChartAreas["Price"].Position.Auto = true;
            //FutOptBasisChartCtrl.ChartAreas["Price"].Position.Height = 80F;
            //FutOptBasisChartCtrl.ChartAreas["Price"].Position.Width = 80F;
            //FutOptBasisChartCtrl.ChartAreas["Price"].Position.X = 5F;
            //FutOptBasisChartCtrl.ChartAreas["Price"].Position.Y = 10F;
            FutOptBasisChartCtrl.ChartAreas["Price"].ShadowColor = System.Drawing.Color.Transparent;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = true;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisX.IsMarginVisible = true;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.IsEndLabelVisible = true;
            FutOptBasisChartCtrl.ChartAreas["Price"].AxisY.IsMarginVisible = true;
            //FutOptBasisChartCtrl.Series[0].Color = Color.Red;
            //DataPointCustomProperties aa = FutOptBasisChartCtrl.Series[0]["PriceUpColor"];
        }

        private void FutOptBasisChartFrm_Load(object sender, EventArgs e)
        {
            InitChart();
            InitTimer();
        }

        private void FutOptBasisChartFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Stop();
        }
    }
}
